import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'


Vue.use(Router)


const routes = [{
			path: '/',
			name: "left",
			meta: {
				title: "首页",
			},
			redirect: '/bookList', // 路由重定向
			component: () => import('@/components/menu/left'),
			children: [
				// {
				// 	path: '/index',
				// 	name: "index",
				// 	meta: {
				// 		title: "首页",
				// 	},
				// 	component: () => import('@/pages/index/index')
				// }, 
				{
					path: '/shop',
					name: "shop",
					meta: {
						title: "商城",
					},
					component: () => import('@/pages/shop/index')
				},
				{
					path: '/activity',
					name: "activity",
					meta: {
						title: "活动管理",
					},
					component: () => import('@/pages/activity/index')
				},
				{
					path: '/banner',
					name: "banner",
					meta: {
						title: "轮播图管理",
					},
					component: () => import('@/pages/activity/banner')
				},
				
				{
					path: '/addBanner',
					name: "addBanner",
					meta: {
						title: "轮播图管理",
					},
					component: () => import('@/pages/activity/addBanner')
				},
				{
					path: '/notice',
					name: "notice",
					meta: {
						title: "通知管理",
					},
					component: () => import('@/pages/activity/notice')
				},
				{
					path: '/addNotice',
					name: "addNotice",
					meta: {
						title: "通知管理",
					},
					component: () => import('@/pages/activity/addNotice')
				},
				{
					path: '/classify',
					name: "classify",
					meta: {
						title: "商品分类",
					},
					component: () => import('@/pages/Mall/classify')
				},

				{
					path: '/addClassify',
					name: "addClassify",
					meta: {
						title: "添加分类",
					},
					component: () => import('@/pages/Mall/addClassify')
				},
				{
					path: '/OrdinaryMall',
					name: "OrdinaryMall",
					meta: {
						title: "普通商品",
					},
					component: () => import('@/pages/Mall/OrdinaryMall')
				},
				{
					path: '/addMall',
					name: "addMall",
					meta: {
						title: "添加商品",
					},
					component: () => import('@/pages/Mall/addMall')
				},
				{
					path: '/bookList',
					name: "bookList",
					meta: {
						title: "订单管理",
					},
					component: () => import('@/pages/book/index')
				},
				{
					path: '/help',
					name: "help",
					meta: {
						title: "帮助中心",
					},
					component: () => import('@/pages/help/index')
					
				},
				{
					path: '/helpDetail',
					name: "helpDetail",
					meta: {
						title: "订单管理",
					},
					component: () => import('@/pages/help/detail')
					
				},
				{
					path: '/kefu',
					name: "kefu",
					meta: {
						title: "客服",
					},
					component: () => import('@/pages/kefu/index')
					
				},
				{
					path: '/user',
					name: "user",
					meta: {
						title: "用户管理",
					},
					component: () => import('@/pages/user/index')
					
				},
		]



	},
	{
		path: '/shopDetail/:pre',
		name: "shopDetail",
		meta: {
			title: "商城详情",

		},
		component: () => import('@/pages/shop/detail')
	}, {
		path: '/login',
		name: "login",
		meta: {
			title: "登录",
			noAuth: true
		},
		component: () => import('@/pages/login/index')
	}, {
		path: '/my',
		name: "my",
		meta: {
			title: "我的",
		},
		component: () => import('@/pages/my/index')
	},
]



const router = new Router({
	mode: 'history',
	routes
})

// 防止路由重复点击导致报错
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push(to) {
	return VueRouterPush.call(this, to).catch(err => err)
}


// 记录面包屑路由
var routeList = store.state.bread;

function recordPage(to, next) {
	var index = -1;
	//  判断路由之前是否存在，并返回位置
	for (var i = 0; i < routeList.length; i++) {
		if (routeList[i].name) {
			if (routeList[i].name == to.name) {
				index = i;
				break;
			}
		}
	}
	if (index !== -1) {
		//   如果存在，则回到重复路由的之前位置
		// routeList.splice(index + 1, routeList.length - index - 1);
	} else {
		//   不存在则加入循环数组内 （注1）
		routeList.push({
			name: to.name,
			path: to.path,
			title: to.meta.title
		});
	}
	store.commit("ADD_BREAD", routeList)

	next()
}

router.beforeEach((to, from, next) => {
	if (to.meta.noAuth) return next()
	if (!store.state.userInfo) return next('/login')

	recordPage(to, next)
})


export default router
